using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DTLAutomateGUI.Controls.LayoutControls;
using DTLAutomateGUI.Controls;
using DTLAutomateGUI.Data;
using System.Drawing;

namespace DTLAutomateGUI.Controls.FormControls
{
	delegate void ModeChangedEvent(CanvasMode mode);

	class LayoutCanvas : Panel
	{
		#region constructors

		#endregion

		#region properties

		private CanvasMode mode;

		public CanvasMode Mode
		{
			get { return mode; }
			set 
			{
				if (this.mode != value)
				{
					mode = value;
					foreach (Control control in this.Controls)
					{
						if (control is LayoutItemControl)
						{
							((LayoutItemControl)control).CurrentCanvasMode = value;
						}
					}
					if (this.modeChanged != null) this.modeChanged(this.mode);
				}
			}
		}

		private CanvasObject objectToPlace;

		public CanvasObject ObjectToPlace
		{
			get { return objectToPlace; }
			set { objectToPlace = value; }
		}

		private DataSetLayout theLayout;
		public DataSetLayout TheLayout
		{
			get { return theLayout; }
			set 
			{ 
				theLayout = value;
				this.initializeLayout();
			}
		}


		#endregion

		#region public methods

		#endregion

		#region events

		public event SelectedEvent selectionChanged;

		public event ModeChangedEvent modeChanged;

		#endregion

		#region private variables

		#endregion

		#region private methods

		private void deselect()
		{
			foreach (Control control in this.Controls)
			{
				if (control is Selectable)
				{
					((Selectable)control).Selected = false;
				}
			}
		}

		private void initializeLayout()
		{
			LayoutItemControl control;
			if (this.theLayout == null) return;
			foreach (DataSetLayout.BlockRow row in this.theLayout.Block.Rows)
			{
				switch (row.type)
				{
					case "SimpleBlock":
						control = new SimpleBlockControl(row);
						control.selectionPerformed += new SelectedEvent(objectToPlace_selectionPerformed);
						this.Controls.Add(control);
						break;
					case "Turnout":
						
						break;
					case "ThreewayTurnout":
						
						break;
					case "SingleCrossover":
						
						break;
					default:
						break;
				}
			}
			foreach (DataSetLayout.BorderRow row in this.theLayout.Border.Rows)
			{
				switch (row.type)
				{
					case "SimpleBorder":
					case "DumbBorder":
						control = new DumbBorderControl(row);
						control.selectionPerformed += new SelectedEvent(objectToPlace_selectionPerformed);
						this.Controls.Add(control);
						break;
					case "SensorBorder":

						break;
					default:
						break;
				}
			}
		}

		#endregion

		#region overriden methods

		public override string ToString()
		{
			return base.ToString();
		}

		protected override void OnMouseClick(MouseEventArgs e)
		{
			LayoutItemControl control;
			DataSetLayout.BlockRow blockRow;
			DataSetLayout.BorderRow borderRow;

			switch (this.mode)
			{
				case CanvasMode.Connect:

					break;
				case CanvasMode.Delete:

					break;
				case CanvasMode.Place:
					switch (this.objectToPlace)
					{
						case CanvasObject.SimpleBlock:
							blockRow = this.theLayout.Block.NewBlockRow();
							this.theLayout.Block.AddBlockRow(blockRow);
							this.theLayout.Block.AcceptChanges();
							blockRow.locationX = e.X;
							blockRow.locationY = e.Y;
							control = new SimpleBlockControl(blockRow);
							control.CurrentCanvasMode = this.Mode;
							control.selectionPerformed += new SelectedEvent(objectToPlace_selectionPerformed);
							this.Controls.Add(control);
							this.Mode = CanvasMode.Select;
							break;
						case CanvasObject.SimpleBorder:
							borderRow = this.theLayout.Border.NewBorderRow();
							this.theLayout.Border.AddBorderRow(borderRow);
							borderRow.locationX = e.X;
							borderRow.locationY = e.Y;
							control = new DumbBorderControl(borderRow);
							control.CurrentCanvasMode = this.Mode;
							control.selectionPerformed += new SelectedEvent(objectToPlace_selectionPerformed);
							this.Controls.Add(control);
							this.Mode = CanvasMode.Select;
							break;
						default:
							break;
					}
					break;
				case CanvasMode.Select:
					this.deselect();
					break;
				default:
					break;
			}
			base.OnMouseClick(e);
		}

		void objectToPlace_selectionPerformed(SelectEventArgs se)
		{
			if (se.action == SelectAction.Select)
			{
				foreach (Control control in this.Controls)
				{
					if ((control is Selectable) && (control != se.source) && (se.source.Selected == true))
					{
						((Selectable)control).Selected = false;
					}
				}
			}
			if (this.selectionChanged != null) this.selectionChanged(se);
		}

		//protected override void OnPaint(PaintEventArgs e)
		//{
		//    foreach (Control control in this.Controls)
		//    {

		//    }
		//    base.OnPaint(e);
		//}

		#endregion

	}
}